package com.ssyt.ec.stu.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ssyt.ec.common.Constant;
import com.ssyt.ec.common.PageModel;
import com.ssyt.ec.common.util.DBUtil;
import com.ssyt.ec.stu.entity.ClassEntity;
import com.ssyt.ec.stu.entity.UserEntity;
import com.ssyt.ec.sys.entity.RoleEntity;

public class UserService {

	/**
	 * 条件查询
	 * 
	 * @param name
	 * @param state
	 * @param clazz
	 * @param role
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @return
	 */
	public PageModel<UserEntity> listUser(String name, String role,
			String clazz, String state, int page, int rows, String sort,
			String order) {
		List<Object> listParam = new ArrayList<>();
		StringBuffer sb = new StringBuffer("SELECT ");
		sb.append("u.id,u.code,u.name,u.sex,u.phone,u.email,u.create_time,u.state,u.memo,");
		sb.append("r.name AS role__name,r.id AS role__id,");
		sb.append("c.name AS class__name,c.id AS class__id ");
		sb.append("FROM sys_user u, sys_role r ,sys_class c ");
		sb.append("WHERE r.id=u.role_id AND c.id=u.class_id ");
		if (StringUtils.isNotBlank(name)) {
			sb.append("AND u.name like ? ");
			listParam.add("%" + name + "%");
		}
		if (StringUtils.isNotBlank(role)) {
			sb.append("AND u.role_id=? ");
			listParam.add(role);
		}
		if (StringUtils.isNotBlank(clazz)) {
			sb.append("AND u.class_id=? ");
			listParam.add(clazz);
		}
		if (StringUtils.isNotBlank(state)) {
			sb.append("AND u.state=? ");
			listParam.add(state);
		} else {
			sb.append("AND u.state NOT IN(3) ");
		}
		sb.append("ORDER BY ");
		sb.append(sort + " " + order);
		sb.append(" LIMIT ?,?");
		listParam.add((page - 1) * rows);
		listParam.add(rows);
		return DBUtil.executeQueryByPage(UserEntity.class, sb.toString(),
				listParam);
	}

	/**
	 * 角色列表
	 * 
	 * @return
	 */
	public List<RoleEntity> listRole() {
		String sql = "SELECT id,name FROM sys_role WHERE id NOT IN(1) AND state=1";
		return DBUtil.executeQuery(RoleEntity.class, sql);
	}

	/**
	 * 班级列表
	 * 
	 * @return
	 */
	public List<ClassEntity> listClass() {
		String sql = "SELECT id,name FROM sys_class ";
		return DBUtil.executeQuery(ClassEntity.class, sql);
	}

	/**
	 * 账号唯一验证
	 * 
	 * @param code
	 * @param paramName
	 * @return
	 */
	public UserEntity uniqueCode(String param, String paramName) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT name FROM sys_user WHERE ");
		sb.append(paramName);
		sb.append("=? LIMIT 1");
		return DBUtil.getUniqueResult(UserEntity.class, sb.toString(), param);
	}

	/**
	 * 新增用户
	 * 
	 * @param userEntity
	 * @param userId
	 * @return
	 */
	public int add(UserEntity userEntity, int userId) {
		List<Object> listParam = new ArrayList<>();
		listParam.add(userEntity.getCode());
		listParam.add(userEntity.getPassword());
		listParam.add(userEntity.getName());
		listParam.add(userEntity.getSex());
		listParam.add(userEntity.getPhone());
		listParam.add(userEntity.getEmail());
		listParam.add(userId);
		listParam.add(userEntity.getRole__id());
		listParam.add(userEntity.getClass__id());
		listParam.add(userEntity.getMemo());

		StringBuffer sb = new StringBuffer();
		sb.append("INSERT INTO sys_user ");
		sb.append(" VALUES (NULL,?,?,?,?,?,?,NOW(),?, NOW(),?,?,1,?);");
		return DBUtil.executeUpdate(sb.toString(), listParam);
	}

	/**
	 * 按id查询单条记录
	 * 
	 * @param id
	 * @return
	 */
	public UserEntity getUser(int id) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT u.id,u.code,u.name,u.sex,u.phone,u.email,u.state,u.memo, ");
		sb.append("r.name AS role__name,r.id AS role__id,");
		sb.append("c.name AS class__name,c.id AS class__id ");
		sb.append("FROM sys_user u,sys_role r,sys_class c ");
		sb.append("WHERE u.role_id=r.id AND u.class_id=c.id ");
		sb.append("AND u.id=?");
		return DBUtil.getUniqueResult(UserEntity.class, sb.toString(), id);
	}

	/**
	 * 用户修改
	 * 
	 * @param listParam
	 * @return
	 */
	public int update(List<Object> listParam) {

		StringBuffer sb = new StringBuffer();
		sb.append("UPDATE sys_user SET ");
		sb.append("name=?,");
		sb.append("sex=?,");
		sb.append("phone=?,");
		sb.append("email=?,");
		sb.append("operate_user_id=?,");
		sb.append("operate_time=NOW(),");
		sb.append("role_id=?,");
		sb.append("class_id=?,");
		sb.append("memo=? ");
		sb.append("WHERE id=?");
		return DBUtil.executeUpdate(sb.toString(), listParam);
	}

	/**
	 * 删除用户
	 * 
	 * @param ids
	 * @return
	 */
	public int deleteUser(String[] ids, int userId) {
		String sql = "UPDATE sys_user SET state=3,operate_user_id=?,operate_time=NOW() WHERE id IN ("
				+ StringUtils.join(ids) + ")";
		return DBUtil.executeUpdate(sql, userId);
	}

	/**
	 * 启用用户
	 * 
	 * @param ids
	 * @return
	 */
	public int open(String[] ids, int userId) {
		String sql = "UPDATE sys_user SET state=1,operate_user_id=?,operate_time=NOW() WHERE id IN ("
				+ StringUtils.join(ids) + ")";
		return DBUtil.executeUpdate(sql, userId);
	}

	/**
	 * 重置密码
	 * 
	 * @param ids
	 * @return
	 */
	public int resetPassword(String[] ids, int userId) {
		String sql = "UPDATE sys_user SET password=?,operate_user_id=?,operate_time=NOW() WHERE id IN ("
				+ StringUtils.join(ids) + ")";
		return DBUtil.executeUpdate(sql, Constant.INIT_PASSWORD, userId);
	}

	/**
	 * 禁用
	 * 
	 * @param ids
	 * @param userId
	 * @return
	 */
	public int close(String[] ids, int userId) {
		String sql = "UPDATE sys_user SET state=2,operate_user_id=?,operate_time=NOW() WHERE id IN ("
				+ StringUtils.join(ids) + ")";
		return DBUtil.executeUpdate(sql, userId);
	}
}
